﻿using System;
using System.Data;
using System.Net;
using System.Windows.Forms;
using CuteSprite.Eme.Configuration;
using HmFramework;
using HmFramework.UI.Utils;
using ResManagerUtils = CuteSprite.Eme.UI.Utils.Resources.ResManager;

namespace CuteSprite.Eme.Server.Configuration
{
	public partial class FrmIPAccess : HmBaseForm
	{
		#region -- 属性 --

		private AdminConfigInfo _IPSettings = null;

		internal AdminConfigInfo IPSettings
		{
			get { return _IPSettings; }
			set { _IPSettings = value as AdminConfigInfo; }
		}

		private DataRow _IPAccessItem = null;

		internal DataRow IPAccessItem
		{
			get { return _IPAccessItem; }
			set
			{
				_IPAccessItem = value as DataRow;
				txtStartIP.Text = _IPAccessItem[AdminConfigInfo._.Column_IPAccess_StartIP].ToString();
				txtEndIP.Text = _IPAccessItem[AdminConfigInfo._.Column_IPAccess_EndIP].ToString();
			}
		}

		private Boolean _IsAddNew = true;

		/// <summary>是否新增记录</summary>
		internal Boolean IsAddNew
		{
			get { return _IsAddNew; }
			set { _IsAddNew = value; }
		}

		#endregion

		#region -- 界面初始化 --

		public FrmIPAccess()
		{
			InitializeComponent();
			InitForm();
		}

		public static FrmIPAccess Create(AdminConfigInfo s)
		{
			FrmIPAccess frm = new FrmIPAccess();
			frm.IsAddNew = true;
			frm.IPSettings = s;
			return frm;
		}

		public static FrmIPAccess Create(AdminConfigInfo s, DataRow dr)
		{
			FrmIPAccess frm = new FrmIPAccess();
			frm.IsAddNew = false;
			frm.IPSettings = s;
			frm.IPAccessItem = dr;
			return frm;
		}

		#endregion

		#region -- 窗体事件 --

		#region - method InitIcon -

		/// <summary>载入窗体图标</summary>
		protected override void InitIcon()
		{
			this.Icon = ResManagerUtils.GetIcon("logo.ico");
		}

		#endregion

		#region - method LoadLocalization -

		/// <summary>载入语言包</summary>
		protected override void LoadLocalization()
		{
			this.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmIPAccess_Caption_From);
			btnOK.Text = UICommonLocalizer.GetString(UICommonStringId.OKButtonText);
			btnCancel.Text = UICommonLocalizer.GetString(UICommonStringId.CancelButtonText);
			gpIPRange.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmIPAccess_Caption_gpIPRange);
			lblStartIP.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmIPAccess_Caption_lblStartIP);
			lblEndIP.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmIPAccess_Caption_lblEndIP);
		}

		#endregion

		#region - method CheckInput -

		protected override Boolean CheckInput()
		{
			String startIP = txtStartIP.Text;
			String endIP = txtEndIP.Text;
			if (MatchHelper.StrIsNullOrEmpty(endIP))
			{
				endIP = startIP;
			}

			try
			{
				IPAddress.Parse(startIP);
			}
			catch
			{
				DialogHelper.ShowWarning(this.Text, EmeConfigLocalizer.GetString(EmeConfigStringId.Message_InvalidStartIP));
				return false;
			}

			try
			{
				IPAddress.Parse(endIP);
			}
			catch
			{
				DialogHelper.ShowWarning(this.Text, EmeConfigLocalizer.GetString(EmeConfigStringId.Message_InvalidEndIP));
				return false;
			}

			return true;
		}

		#endregion

		#endregion

		#region -- Events Handling --

		private void btnOK_Click(Object sender, EventArgs e)
		{
			Boolean flag = CheckInput();
			if (!flag) { return; }

			if (IsAddNew)
			{
				var tbIP = IPSettings.GetTable(AdminConfigInfo._.Table_IPAccess);
				DataRow dr = tbIP.NewRow();
				dr[AdminConfigInfo._.Column_IPAccess_StartIP] = txtStartIP.Text;
				dr[AdminConfigInfo._.Column_IPAccess_EndIP] = txtEndIP.Text;
				tbIP.Rows.Add(dr);
			}
			else
			{
				_IPAccessItem[AdminConfigInfo._.Column_IPAccess_StartIP] = txtStartIP.Text;
				_IPAccessItem[AdminConfigInfo._.Column_IPAccess_EndIP] = txtEndIP.Text;
			}

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void btnCancel_Click(Object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion
	}
}
